package com.gggd.aopdemo.aop;

import com.gggd.aopdemo.util.WebApiResult;
import com.gggd.aopdemo.vo.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author srx
 * @date 2021/12/24 17:57
 */
@Aspect
@Component
@Order(0)
public class MyAop03 {
    @Pointcut("@annotation(com.gggd.aopdemo.anno.MyAnnotation)")
    private void myAop03Pointcut() {
    }

    @Around("myAop03Pointcut()")
    public Object myAop03(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("===================第二个切面===================：");

        //获取请求参数
        Object[] objects = joinPoint.getArgs();
        Integer uid = ((User) objects[0]).getUid();
        String name = ((User) objects[0]).getName();
        System.out.println("uid1->>>>>>>>>>>>>>>>>>>>>>" + uid);
        System.out.println("name1->>>>>>>>>>>>>>>>>>>>>>" + name);

        // name不是管理员则抛出异常
        if (!name.equals("admin")) {
            return WebApiResult.error("非法用户");
        }
        return joinPoint.proceed();
    }
}
